home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / jid.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  6KB  |  206 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: jid.py 648 2006-08-26 20:09:37Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import re
  7. import weakref
  8. import warnings
  9. from encodings import idna
  10. from pyxmpp.xmppstringprep import nodeprep, resourceprep
  11. from pyxmpp.exceptions import JIDError
  12. node_invalid_re = re.compile(u'["&\'/:<>@\\s\\x00-\\x19]', re.UNICODE)
  13. resource_invalid_re = re.compile(u'[\\s\\x00-\\x19]', re.UNICODE)
  14.  
  15. def are_domains_equal(a, b):
  16.     a = idna.ToASCII(a)
  17.     b = idna.ToASCII(b)
  18.     return a.lower() == b.lower()
  19.  
  20.  
  21. class JID(object):
  22.     cache = weakref.WeakValueDictionary()
  23.     __slots__ = [
  24.         'node',
  25.         'domain',
  26.         'resource',
  27.         '__weakref__']
  28.     
  29.     def __new__(cls, node_or_jid = None, domain = None, resource = None, check = True):
  30.         if isinstance(node_or_jid, JID):
  31.             return node_or_jid
  32.         
  33.         if domain is None and resource is None:
  34.             obj = cls.cache.get(node_or_jid)
  35.             if obj:
  36.                 return obj
  37.             
  38.         else:
  39.             obj = None
  40.         if obj is None:
  41.             obj = object.__new__(cls)
  42.         
  43.         if node_or_jid:
  44.             node_or_jid = unicode(node_or_jid)
  45.         
  46.         if node_or_jid:
  47.             if u'@' in node_or_jid or u'/' in node_or_jid:
  48.                 obj._JID__from_unicode(node_or_jid)
  49.                 cls.cache[node_or_jid] = obj
  50.             elif domain is None and resource is None:
  51.                 if node_or_jid is None:
  52.                     raise JIDError, 'At least domain must be given'
  53.                 
  54.                 domain = node_or_jid
  55.                 node_or_jid = None
  56.             
  57.         if check:
  58.             obj._JID__set_node(node_or_jid)
  59.             obj._JID__set_domain(domain)
  60.             obj._JID__set_resource(resource)
  61.         else:
  62.             object.__setattr__(obj, 'node', node_or_jid)
  63.             object.__setattr__(obj, 'domain', domain)
  64.             object.__setattr__(obj, 'resource', resource)
  65.         return obj
  66.  
  67.     
  68.     def __setattr__(self, name, value):
  69.         raise RuntimeError, 'JID objects are immutable!'
  70.  
  71.     
  72.     def __from_unicode(self, s, check = True):
  73.         s1 = s.split(u'/', 1)
  74.         s2 = s1[0].split(u'@', 1)
  75.         if len(s2) == 2:
  76.             if check:
  77.                 self._JID__set_node(s2[0])
  78.                 self._JID__set_domain(s2[1])
  79.             else:
  80.                 object.__setattr__(self, 'node', s2[0])
  81.                 object.__setattr__(self, 'domain', s2[1])
  82.         elif check:
  83.             self._JID__set_domain(s2[0])
  84.         else:
  85.             object.__setattr__(self, 'domain', s2[0])
  86.         object.__setattr__(self, 'node', None)
  87.         if len(s1) == 2:
  88.             if check:
  89.                 self._JID__set_resource(s1[1])
  90.             else:
  91.                 object.__setattr__(self, 'resource', s1[1])
  92.         else:
  93.             object.__setattr__(self, 'resource', None)
  94.         if not self.domain:
  95.             raise JIDError, 'Domain is required in JID.'
  96.         
  97.  
  98.     
  99.     def __set_node(self, s):
  100.         if s:
  101.             s = unicode(s)
  102.             s = nodeprep.prepare(s)
  103.             if len(s.encode('utf-8')) > 1023:
  104.                 raise JIDError, 'Node name too long'
  105.             
  106.         else:
  107.             s = None
  108.         object.__setattr__(self, 'node', s)
  109.  
  110.     
  111.     def __set_domain(self, s):
  112.         if s is None:
  113.             raise JIDError, 'Domain must be given'
  114.         
  115.         if s:
  116.             s = unicode(s)
  117.         
  118.         s = idna.nameprep(s)
  119.         if len(s.encode('utf-8')) > 1023:
  120.             raise JIDError, 'Domain name too long'
  121.         
  122.         object.__setattr__(self, 'domain', s)
  123.  
  124.     
  125.     def __set_resource(self, s):
  126.         if s:
  127.             s = unicode(s)
  128.             s = resourceprep.prepare(s)
  129.             if len(s.encode('utf-8')) > 1023:
  130.                 raise JIDError, 'Resource name too long'
  131.             
  132.         else:
  133.             s = None
  134.         object.__setattr__(self, 'resource', s)
  135.  
  136.     
  137.     def __str__(self):
  138.         return self.as_utf8()
  139.  
  140.     
  141.     def __unicode__(self):
  142.         return self.as_unicode()
  143.  
  144.     
  145.     def __repr__(self):
  146.         return '<JID: %r>' % self.as_unicode()
  147.  
  148.     
  149.     def as_utf8(self):
  150.         return self.as_unicode().encode('utf-8')
  151.  
  152.     
  153.     def as_string(self):
  154.         warnings.warn('JID.as_string() is deprecated. Use unicode() or `as_utf8` instead.', DeprecationWarning, stacklevel = 1)
  155.         return self.as_utf8()
  156.  
  157.     
  158.     def as_unicode(self):
  159.         r = self.domain
  160.         if self.node:
  161.             r = self.node + u'@' + r
  162.         
  163.         if self.resource:
  164.             r = r + u'/' + self.resource
  165.         
  166.         if not JID.cache.has_key(r):
  167.             JID.cache[r] = self
  168.         
  169.         return r
  170.  
  171.     
  172.     def bare(self):
  173.         return JID(self.node, self.domain, check = False)
  174.  
  175.     
  176.     def __eq__(self, other):
  177.         if other is None:
  178.             return False
  179.         elif type(other) in (str, unicode):
  180.             
  181.             try:
  182.                 other = JID(other)
  183.             return False
  184.  
  185.         elif not isinstance(other, JID):
  186.             return False
  187.         
  188.         if self.node == other.node and are_domains_equal(self.domain, other.domain):
  189.             pass
  190.         return self.resource == other.resource
  191.  
  192.     
  193.     def __ne__(self, other):
  194.         return not self.__eq__(other)
  195.  
  196.     
  197.     def __cmp__(self, other):
  198.         a = self.as_unicode()
  199.         return cmp(a, other)
  200.  
  201.     
  202.     def __hash__(self):
  203.         return hash(self.node) ^ hash(self.domain) ^ hash(self.resource)
  204.  
  205.  
  206.